package com.wmx.yuanyuan.controller;

import com.wmx.yuanyuan.util.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制器层——@RequestMapping 请求映射
 * - @GET 	 向特定资源发出请求（请求指定页面信息，并返回实体主体）
 * - @POST	 向指定资源提交数据进行处理请求（提交表单、上传文件）
 * - @PUT	 向指定资源位置上上传其最新内容
 * - @DELETE 请求服务器删除指定的资源
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/4/4 16:09
 */
@RestController
@RequestMapping(value = "visits")
public class VisitsController {

    /**
     * http://localhost:8080/visits/test1
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "test1", method = RequestMethod.GET)
    public String visitsTest1(HttpServletRequest request) {
        return request.getRequestURL().toString();
    }

    /**
     * http://localhost:8080/visits/test2, http://localhost:8080/visits/test3 都会进入
     *
     * @param request
     * @return
     */
    @RequestMapping(value = {"test2", "test3"}, method = RequestMethod.GET)
    public String visitsTest23(HttpServletRequest request) {
        return request.getRequestURL().toString();
    }

    /**
     * http://localhost:8080/visits/test4?token=98, 请求必须携带参数 token，否则匹配不上，进入不了方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "test4", params = {"token"}, method = RequestMethod.GET)
    public String visitsTest4(HttpServletRequest request) {
        return request.getRequestURL().toString() + "?" + request.getParameter("token");
    }

    /**
     * 下面不同方式传参的，后台都可以用字符串接收，如："a,b,c,d"
     * http://localhost:8080/visits/users1?ids=120DY
     * http://localhost:8080/visits/users1?ids=120DY,230DY
     * http://localhost:8080/visits/users1?ids=120DY&ids=230DY&ids=34LP
     *
     * @param ids
     * @return
     */
    @GetMapping("users1")
    public ResultData getUsers1(@RequestParam(value = "ids") String ids) {
        return ResultData.success(ids);
    }

    /**
     * 下面不同方式的传参，后台都可以用集合接收，如：["01","02","03"]
     * http://localhost:8080/visits/users2?ids=01&codes=001
     * http://localhost:8080/visits/users2?ids=01&ids=02&codes=001&codes=002
     * http://localhost:8080/visits/users2?ids=01,02,03&codes=001,002,003,004
     * <p>
     * 注意：参数内容比较多时，不建议使用查询参数(?x=y)传输，因为数据量一旦超过限制，就会请求报错。
     * 比如左侧一颗单位树，上面有一个全选按钮，一旦此时传递的单位编码拼接的太长，请求就会报错。
     * 此时建议改为POST请求，建内容放在Body请求体中传输。
     *
     * @param ids
     * @param codes
     * @return
     */
    @GetMapping("users2")
    public ResultData getUsers2(@RequestParam(value = "ids") List<String> ids,
                                @RequestParam(value = "codes") List<String> codes) {
        Map<String, Object> dataMap = new HashMap<>(8);
        dataMap.put("ids", ids);
        dataMap.put("codes", codes);

        // {"code":200,"message":"操作成功","data":{"ids":["01","02"],"codes":["001","002"]}}
        // {"code":200,"message":"操作成功","data":{"ids":["01","02","03"],"codes":["001","002","003","004"]}}
        return ResultData.success(dataMap);
    }

    /**
     * http://localhost:8080/visits/test5/his, http://localhost:8080/visits/test5/5x，路径结尾必须有路径变量
     *
     * @param type
     * @param request
     * @return
     */
    @RequestMapping(value = "test5/{type}", method = RequestMethod.POST)
    public String visitsTest5(@PathVariable(value = "type") String type, HttpServletRequest request) {
        return request.getRequestURL().toString() + "\t" + type;
    }

}
